<template>
  <div v-if="AnimeClass && AnimeClass.length > 0" class="pet-list-container">
    <!-- 分类导航栏 -->
    <div class="category-nav">
      <div
        v-for="(category, index) in AnimeClass"
        :key="index"
        class="nav-item"
        @click="scrollToCategory(category)"
      >
        {{ category }}
      </div>
    </div>
    <!-- 分类标题 -->
    <div v-for="(category, index) in AnimeClass" :key="index" class="category-section" :id="`category-${category}`">
      <!-- 分类名称 -->
      <div class="category-title">
        {{ category }}
      </div>
      <!-- 宠物卡片列表 -->
      <el-row :gutter="20" class="pet-card-list">
        <el-col
          v-for="(pet, index2) in filteredAnimals(category)"
          :key="index2"
          :xs="12" :sm="8" :md="6" :lg="4" :xl="3"
        >
          <el-card class="pet-card" shadow="hover" @click="openWiki(pet.id)">
            <el-image class="pet-image" fit="cover" :src="pet.imgPath" :alt="pet.全名"></el-image>
            <div class="pet-name">{{ pet.全名 }}</div>
          </el-card>
        </el-col>
      </el-row>
    </div>
  </div>
  <div v-else class="loading-message">
    加载中...
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue';
import { useRouter } from 'vue-router';
import { getAllAnimeList, getAnimeClass } from '@/api/home';
import { getimage } from '@/api/image';

const router = useRouter();
const animals = ref([]); // 宠物数据
const AnimeClass = ref([]); // 分类数据

// 获取分类数据
const syncAnimeClass = async () => {
  const response = await getAnimeClass();
  AnimeClass.value = response.data.data.slice(1); // 去掉第一个空分类
};

// 获取宠物数据
const syncAnimeList = async () => {
  const res = await getAllAnimeList();
  animals.value = res.data.data;
  animals.value.forEach((pet) => {
    pet.imgPath = getimage(pet.imgPath); // 处理图片路径
  });
};

// 跳转到宠物详情页
const openWiki = (id) => {
  router.push({ name: 'Baike', params: { id } });
};

// 根据分类过滤宠物
const filteredAnimals = (category) => {
  if (!animals.value) return [];
  return animals.value.filter((pet) => pet.分类 === category);
};

// 初始化数据
onMounted(() => {
  syncAnimeClass().then(() => syncAnimeList());
});

// 滚动到指定分类
const scrollToCategory = (category) => {
  const element = document.getElementById(`category-${category}`);
  if (element) {
    element.scrollIntoView({ behavior: 'smooth', block: 'start' });
  }
};
</script>

<style scoped>
.pet-list-container {
  padding: 40px 20px;
  background: linear-gradient(135deg, #f6f8fc 0%, #e9f0f7 100%);
  min-height: 100vh;
  position: relative;
}

.pet-list-container::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-image: url("data:image/svg+xml,%3Csvg width='60' height='60' viewBox='0 0 60 60' xmlns='http://www.w3.org/2000/svg'%3E%3Cg fill='none' fill-rule='evenodd'%3E%3Cg fill='%23e0e8f5' fill-opacity='0.4'%3E%3Cpath d='M36 34v-4h-2v4h-4v2h4v4h2v-4h4v-2h-4zm0-30V0h-2v4h-4v2h4v4h2V6h4V4h-4zM6 34v-4H4v4H0v2h4v4h2v-4h4v-2H6zM6 4V0H4v4H0v2h4v4h2V6h4V4H6z'/%3E%3C/g%3E%3C/g%3E%3C/svg%3E");
  pointer-events: none;
  opacity: 0.6;
  z-index: 0;
}

.category-section {
  margin-bottom: 50px;
  position: relative;
  z-index: 1;
}

.category-title {
  font-size: 28px;
  font-weight: bold;
  margin-bottom: 30px;
  padding: 15px 30px;
  background: linear-gradient(135deg, #4a90e2, #2c5282);
  color: #fff;
  border-radius: 15px;
  box-shadow: 0 8px 20px rgba(44, 82, 130, 0.25);
  display: inline-block;
  position: relative;
  overflow: hidden;
  transition: all 0.3s ease;
}

.category-title::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: linear-gradient(45deg, rgba(255,255,255,0.1), rgba(255,255,255,0));
  transform: translateX(-100%);
  transition: transform 0.6s ease;
}

.category-title:hover::before {
  transform: translateX(100%);
}

.pet-card-list {
  margin: 0 -10px;
}

.pet-card {
  position: relative;
  border-radius: 20px;
  overflow: hidden;
  transition: all 0.4s cubic-bezier(0.4, 0, 0.2, 1);
  margin: 15px;
  background-color: #fff;
  border: 1px solid rgba(0, 0, 0, 0.05);
}

.pet-card:hover {
  transform: translateY(-8px) scale(1.02);
  box-shadow: 0 15px 30px rgba(0, 0, 0, 0.15);
  border-color: rgba(74, 144, 226, 0.3);
}

.pet-card:hover .pet-image {
  transform: scale(1.1);
}

.pet-image {
  width: 100%;
  height: 200px;
  display: block;
  transition: transform 0.6s ease;
  object-fit: cover;
}

.pet-name {
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  background: linear-gradient(to top, rgba(0, 0, 0, 0.8), rgba(0, 0, 0, 0));
  color: #fff;
  text-align: center;
  padding: 20px 10px 10px;
  font-size: 16px;
  font-weight: bold;
  letter-spacing: 0.5px;
  transform: translateY(0);
  transition: transform 0.3s ease;
}

.pet-card:hover .pet-name {
  transform: translateY(-5px);
}

.loading-message {
  text-align: center;
  font-size: 18px;
  color: #666;
  padding: 50px;
}

/* 响应式设计 */
@media (max-width: 1200px) {
  .pet-list-container {
    padding: 30px 15px;
  }
  .category-title {
    font-size: 24px;
    padding: 12px 24px;
  }
}

@media (max-width: 992px) {
  .pet-list-container {
    padding: 25px 12px;
  }
  .category-title {
    font-size: 22px;
    padding: 10px 20px;
  }
  .pet-card {
    margin: 12px;
  }
  .pet-image {
    height: 180px;
  }
}

@media (max-width: 768px) {
  .pet-list-container {
    padding: 20px 10px;
  }
  .category-title {
    font-size: 20px;
    padding: 8px 16px;
    margin-bottom: 20px;
  }
  .category-nav {
    padding: 10px;
    margin: -20px -10px 15px;
  }
  .nav-item {
    padding: 6px 15px;
    margin: 0 6px;
    font-size: 14px;
  }
  .pet-card {
    margin: 10px;
  }
  .pet-image {
    height: 150px;
  }
  .pet-name {
    font-size: 14px;
    padding: 8px;
  }
}

@media (max-width: 576px) {
  .pet-list-container {
    padding: 15px 8px;
  }
  .category-title {
    font-size: 18px;
    padding: 6px 12px;
    margin-bottom: 15px;
  }
  .category-nav {
    padding: 8px;
    margin: -15px -8px 12px;
  }
  .nav-item {
    padding: 4px 12px;
    margin: 0 4px;
    font-size: 13px;
  }
  .pet-card {
    margin: 8px;
  }
  .pet-image {
    height: 130px;
  }
  .pet-name {
    font-size: 13px;
    padding: 6px;
  }
}

.category-nav {
  position: sticky;
  top: 0;
  display: flex;
  overflow-x: auto;
  background: rgba(255, 255, 255, 0.95);
  padding: 15px;
  margin: -40px -20px 20px;
  z-index: 10;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
  -webkit-overflow-scrolling: touch;
  scrollbar-width: none;
}

.category-nav::-webkit-scrollbar {
  display: none;
}

.nav-item {
  flex: 0 0 auto;
  padding: 8px 20px;
  margin: 0 8px;
  background: linear-gradient(135deg, #4a90e2, #2c5282);
  color: white;
  border-radius: 20px;
  cursor: pointer;
  transition: all 0.3s ease;
  white-space: nowrap;
  font-weight: 500;
}

.nav-item:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(74, 144, 226, 0.3);
}
</style>